package com.xidian.zhiancloud.controller.riskcontrol;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.riskcontrol.TRiskTrainingRecordList;
import com.xidian.zhiancloud.model.riskcontrol.TrainingRiskStatusList;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.riskcontrol.TtrainingRiskStatusService;
import com.xidian.zhiancloud.util.api.ExcelUtil2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风险管控-风险培训
 */
@RestController
@RequestMapping("/ttrisksta")
public class TtrainingRiskStatusController extends BaseController {

    @Autowired
    TtrainingRiskStatusService ttrainingRiskStatusService;

    /**
     * 风险培训列表
     * @param reqdata 参数集合
     * @param state 状态
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getTtrainingRiskStatusList")
    public ResultDTO getTtrainingRiskStatusList(BaseReqQuery reqdata,
           @RequestParam(value="state",required=false)String state){
        Long companyId=this.getSessionUserCompanyId();
        Page<TrainingRiskStatusList> pagedata =
                ttrainingRiskStatusService.getTtrainingRiskStatusList
                (reqdata,companyId,state);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("datalist", pagedata);
        data.put("PageNum", pagedata.getPageNum());
        data.put("PageSize", pagedata.getPageSize());
        data.put("PageTotal", pagedata.getPages());
        data.put("total", pagedata.getTotal());
        return ResultDTO.successOf(data, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 风险培训-添加
     * @param
     * @param
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/addTtrainingRiskStatus")
    public ResultDTO addTtrainingRiskStatus(TrainingRiskStatusList trainRiskSta){
        Long companyId=this.getSessionUserCompanyId();
        trainRiskSta.setCompanyId(companyId);
        ttrainingRiskStatusService.addTtrainingRiskStatus(trainRiskSta);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 风险培训-编辑
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/updateTtrainingRiskStatus")
    public ResultDTO updateTtrainingRiskStatus(@RequestParam("id") String id,
           TrainingRiskStatusList trainRiskSta){
        ttrainingRiskStatusService.updateTtrainingRiskStatus(id,trainRiskSta);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 风险培训-删除
     * @param id 风险培训表ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/deleteByTtraRStatus")
    public ResultDTO deleteByTtraRStatus(@RequestParam("id") String id){
        ttrainingRiskStatusService.deleteByTtraRStatus(id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 风险培训-查看
     * @param
     * @param id 风险培训表ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getByTtraRStatus")
    public ResultDTO getByTtraRStatus(@RequestParam("id") String id){
        List<TrainingRiskStatusList> trainingRiskStatus = ttrainingRiskStatusService
                .getByTtraRStatus(id);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("trainingRiskStatus", trainingRiskStatus);
        return ResultDTO.successOf(data,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加培训记录
     * @param trainingRiskStatusId 风险培训ID
     * @param trainRecord 培训记录
     * @return 返回ResultDTO对象
     */
    @RequestMapping("/addTrainRecord")
    public ResultDTO insertTrainRecord(@RequestParam("trainingRiskStatusId")
            Integer trainingRiskStatusId,TRiskTrainingRecordList trainRecord){
            trainRecord.setCompanyId(this.getSessionUserCompanyId());
            trainRecord.setTrainingRiskStatusId(trainingRiskStatusId);
            ttrainingRiskStatusService.insertTrainRecord(trainRecord);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     *  -导出风险培训列表
     */
    @GetMapping("/exportTtrainingRiskStatusData")
    public void exportTtrainingRiskStatusData (HttpServletResponse response,
          @RequestParam(value="id",required=false ,defaultValue="0") String id)
            throws IOException {
        Long companyId=this.getSessionUserCompanyId();
        Page<TrainingRiskStatusList> pagedata = ttrainingRiskStatusService
                .getExportTtrainingRiskStatusList(companyId,id);
        List<TrainingRiskStatusList> list = pagedata.getResult();
        ExcelUtil2<TrainingRiskStatusList> util = new
                ExcelUtil2<TrainingRiskStatusList>(TrainingRiskStatusList.class);
        util.exportExcel(response, list, "风险培训");
    }


    /**
     * 发布培训计划
     * @param id 风险培训ID
     * @return 返回ResultDTO对象
     */
    @RequestMapping("/publishTrainRecord")
    public ResultDTO publishTrainRecord(@RequestParam("id") String id){
        Long userId = this.getSessionUserid();
        ttrainingRiskStatusService.publishTrainRecord(userId,id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    

}
